// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class Test_Project_Funding_Pool_Change {
	static testMethod void TestProjectRestrictedFundCreated() {	 
		Id OppId;
		Id FundPoolId;
		
		Opportunity firstOpp = new Opportunity (Name='test opp',StageName='Prospecting',CloseDate=System.today());
		insert firstOpp;
		OppId = firstOpp.id;
		
		Restricted_Fund_Pool__c firstFundPool = new Restricted_Fund_Pool__c(
			Name='First fund pool',
			Amount__c=10000
		);
		insert firstFundPool;
		FundPoolId = firstFundPool.id;
		
		Restricted_Fund_for_Project__c firstRestrictedFund = new Restricted_Fund_for_Project__c (
			Opportunity__c=OppId,
			Amount__c=600,
			Restricted_Fund_Pool__c=FundPoolId
		);
		insert firstRestrictedFund;
		Opportunity[] OppAfterRestrictedFund = [select Total_Restricted_Funds__c from Opportunity where Id =:OppId];
		System.assertEquals(600,OppAfterRestrictedFund[0].Total_Restricted_Funds__c);
		
		Restricted_Fund_Pool__c[] FundPoolAfterFirstAllocation = [select Amount_Allocated__c from Restricted_Fund_Pool__c where Id =:FundPoolId];
		System.assertEquals(600,FundPoolAfterFirstAllocation[0].Amount_Allocated__c);
		
		Restricted_Fund_for_Project__c secondRestrictedFund = new Restricted_Fund_for_Project__c (
			Opportunity__c=OppId,
			Amount__c=800,
			Restricted_Fund_Pool__c=FundPoolId
		);
		insert secondRestrictedFund;
		
		Opportunity[] OppAfter2ndRestrictedFund = [select Total_Restricted_Funds__c from Opportunity where Id =:OppId];
		System.assertEquals(1400,OppAfter2ndRestrictedFund[0].Total_Restricted_Funds__c);
		
		Restricted_Fund_Pool__c[] FundPoolAfterSecondAllocation = [select Amount_Allocated__c from Restricted_Fund_Pool__c where Id =:FundPoolId];
		System.assertEquals(1400,FundPoolAfterSecondAllocation[0].Amount_Allocated__c);
		
		Restricted_Fund_for_Project__c thirdRestrictedFund = new Restricted_Fund_for_Project__c (
			Opportunity__c=OppId,
			Amount__c=null,
			Restricted_Fund_Pool__c=FundPoolId
		);
		insert thirdRestrictedFund;
		
		Opportunity[] OppAfterNullRestrictedFund = [select Total_Restricted_Funds__c from Opportunity where Id =:OppId];
		System.assertEquals(1400,OppAfterNullRestrictedFund[0].Total_Restricted_Funds__c);
		
		secondRestrictedFund.Amount__c=900;
		update secondRestrictedFund;
		
		Opportunity[] OppAfter2ndRestrictedFundChange = [select Total_Restricted_Funds__c from Opportunity where Id =:OppId];
		System.assertEquals(1500,OppAfter2ndRestrictedFundChange[0].Total_Restricted_Funds__c);
		
		Restricted_Fund_Pool__c[] FundPoolAfterSecondAllocationChange = [select Amount_Allocated__c from Restricted_Fund_Pool__c where Id =:FundPoolId];
		System.assertEquals(1500,FundPoolAfterSecondAllocationChange[0].Amount_Allocated__c);
		
		
		//delete firstTaskLog;
		delete secondRestrictedFund;
		
		Opportunity[] OppAfterRestrictedFundDelete = [select Total_Restricted_Funds__c from Opportunity where Id =:OppId];
		System.assertEquals(600,OppAfterRestrictedFundDelete[0].Total_Restricted_Funds__c);
		
		Restricted_Fund_Pool__c[] FundPoolAfterSecondAllocationDeletion = [select Amount_Allocated__c from Restricted_Fund_Pool__c where Id =:FundPoolId];
		System.assertEquals(600,FundPoolAfterSecondAllocationDeletion[0].Amount_Allocated__c);
		
		//delete firstTaskLog;
		delete firstRestrictedFund;
		
		Opportunity[] OppAfter1stRestrictedFundDelete = [select Total_Restricted_Funds__c from Opportunity where Id =:OppId];
		System.assertEquals(0,OppAfter1stRestrictedFundDelete[0].Total_Restricted_Funds__c);
		
		Restricted_Fund_Pool__c[] FundPoolAfterFirstAllocationDeletion = [select Amount_Allocated__c from Restricted_Fund_Pool__c where Id =:FundPoolId];
		System.assertEquals(0,FundPoolAfterFirstAllocationDeletion[0].Amount_Allocated__c);
		
	}
}